﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Watson.TemporaryFilesDeleter
{
    /// <summary>
    /// Provides information about a temporary files location.
    /// </summary>
    public class FilesLocationInfo
    {
        #region Private Fields        
        
        /// <summary>
        /// The number of files contained within the temporary files location
        /// (including the files contained within the subfolders).
        /// </summary>
        private int numberOfFiles;

        /// <summary>
        /// The number of subfolders contained within the temporary files
        /// location.
        /// </summary>
        private int numberOfFolders;

        /// <summary>
        /// Gets the size of the files contained within the temporary files
        /// location in bytes (including the files contained within the
        /// subfolders).
        /// </summary>
        private long sizeOfFiles;

        /// <summary>
        /// The path to the temporary files location.
        /// </summary>
        private string path;
        
        #endregion

        #region Public Properties
        
        /// <summary>
        /// Gets the number of files contained within the temporary files
        /// location (including the files contained within the subfolders).
        /// </summary>
        public int NumberOfFiles
        {
            get { return numberOfFiles; }
        }

        /// <summary>
        /// Gets the number of subfolders contained within the temporary files
        /// location.
        /// </summary>
        public int NumberOfFolders
        {
            get { return numberOfFolders; }
        }

        /// <summary>
        /// Gets the size of the files contained within the temporary files
        /// location in bytes (including the files contained within the
        /// subfolders).
        /// </summary>
        public long SizeOfFiles
        {
            get { return sizeOfFiles; }
        }

        /// <summary>
        /// Gets or sets the path to the temporary files location.
        /// </summary>
        public string Path
        {
            get
            {
                return path;
            }
            set
            {
                path = value;
                
                ObtainInfo();
            }
        }

        #endregion

        #region Private Methods
        
        /// <summary>
        /// Obtains information about the temporary files in the current path.
        /// </summary>
        private void ObtainInfo()
        {
            // Reset counters.
            this.numberOfFiles   = 0;
            this.numberOfFolders = 0;
            this.sizeOfFiles     = 0;
            
            
            // Obtain information about files.
            string[] files = 
                Directory.GetFiles(this.path,
                                   "*",
                                   SearchOption.AllDirectories);

            foreach (var file in files)
            {
                this.numberOfFiles += 1;
                
                FileInfo fileInfo = new FileInfo(file);
                this.sizeOfFiles += fileInfo.Length;
            }


            // Obtain information about folders.
            string[] directories = 
                Directory.GetDirectories(this.path,
                                         "*",
                                         SearchOption.AllDirectories);

            this.numberOfFolders = directories.Length;
        }

        #endregion

        #region Public Constructors
        
        /// <summary>
        /// Initialises a new instance of the FilesLocationInfo class.
        /// </summary>
        public FilesLocationInfo()
        { }

        /// <summary>
        /// Initialises a new instance of the FilesLocationInfo class
        /// specifying a value for the path.
        /// </summary>
        public FilesLocationInfo(string path)
        {
            this.path = path;

            ObtainInfo();
        }

        #endregion
    }
}
